<?php
//模块插件类
//插件功能类，由系统调用，实现具体功能。
defined('BY_JYA') or exit('error');
class core_jy_invitation extends plugin{
	public function createImage($imgurl) 
	{
		
		$resp = cfc('http')->ihttp_request($imgurl);
		if (($resp['code'] == 200) && !(empty($resp['content']))) 
		{
			return imagecreatefromstring($resp['content']);
		}
		$i = 0;
		while ($i < 3) 
		{
			$resp = cfc('http')->ihttp_request($imgurl);
			if (($resp['code'] == 200) && !(empty($resp['content']))) 
			{
				return imagecreatefromstring($resp['content']);
			}
			++$i;
		}
		return '';
	}
	public function mergeImage($target, $data, $imgurl) 
	{
		$img = $this->createImage($imgurl);
		$w = imagesx($img);
		$h = imagesy($img);
		imagecopyresized($target, $img, $data['left'], $data['top'], 0, 0, $data['width'], $data['height'], $w, $h);
		imagedestroy($img);
		return $target;
	}
	public function mergeText($target, $data, $text) 
	{
		$font = ROOT_D . '/addons/jy_invitation/resource/msyh.ttf';
		$colors = $this->hex2rgb($data['color']);
		$color = imagecolorallocate($target, $colors['red'], $colors['green'], $colors['blue']);
		if(!$data['size']){
			$data['size']='17';
		}
		imagettftext($target, $data['size'], 0, $data['left'], $data['top'] + $data['size'], $color, $font, $text);
		return $target;
	}
	public function hex2rgb($colour) 
	{
		if ($colour[0] == '#') 
		{
			$colour = substr($colour, 1);
		}
		if (strlen($colour) == 6) 
		{
			list($r, $g, $b) = array($colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5]);
		}
		else if (strlen($colour) == 3) 
		{
			list($r, $g, $b) = array($colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2]);
		}
		else 
		{
			return false;
		}
		$r = hexdec($r);
		$g = hexdec($g);
		$b = hexdec($b);
		return array('red' => $r, 'green' => $g, 'blue' => $b);
	}
	public function resizejpg($imgsrc,$path,$file2,$imgwidth,$imgheight,$posterid=0){
		global $_AR;
		$arr = getimagesize($imgsrc);   
		$imgsrc=$this->createImage($imgsrc);		
		$imgWidth = $imgwidth;
		$imgHeight = $imgheight;
		//$imgsrc = imagecreatefromstring($imgsrc);
		$image = imagecreatetruecolor($imgWidth, $imgHeight);  //创建一个彩色的底图
		imagecopyresampled($image, $imgsrc, 0, 0, 0, 0,$imgWidth,$imgHeight,$arr[0], $arr[1]);
		imagepng($image, $path.$file2);
		imagedestroy($image);
		if($_SESSION['fromh5wxapp'] || $_SESSION['os']=='wxapp'){
			$img =  ATTACHMENT_URL. 'images/jy_invitation/' . $_SESSION['uniacid'] .  '/' .$posterid. '/wxapp/' . $file2;
		}else{
			$img =  ATTACHMENT_URL. 'images/jy_invitation/' . $_SESSION['uniacid'] .  '/' .$posterid. '/mobile/' . $file2;
		}		
		return $img;
	}
	public function createPoster($poster, $qr, $updata)
	{
		global $_AR;
		if($_SESSION['fromh5wxapp'] || $_SESSION['os']=='wxapp'){
			$path = ROOT_AT .'images/jy_invitation/' . $_SESSION['uniacid'] . '/' .$poster['id']. '/wxapp/';
		}else{
			$path = ROOT_AT .'images/jy_invitation/' . $_SESSION['uniacid'] . '/' .$poster['id']. '/mobile/';
		}
		if (!(is_dir($path))) 
		{
			cfc('files')->mkdirs ($path);
		}
		$data=$poster['data'];
		$md5 = $_SESSION['uid'].'_'.$poster['k'];
		$file = $md5 . '.png';
		$file2 = $md5 . 'bg.png';
		if (is_file($path . $file)) 
		{
			if($_SESSION['fromh5wxapp'] || $_SESSION['os']=='wxapp'){
				$img = 'images/jy_invitation/' . $_SESSION['uniacid'] . '/' .$poster['id']. '/wxapp/' . $file;
			}else{
				$img = 'images/jy_invitation/' . $_SESSION['uniacid'] . '/' .$poster['id']. '/mobile/' . $file;
			}
			//return $img;	
		}
		if (!(is_file($path . $file)) || true) 
		{
			set_time_limit(0);
			@ini_set('memory_limit', '256M');
			$target = imagecreatetruecolor(640, 1008);
			$bg1 = tomedia($poster['bg']);
			//file_put_contents(ROOT_D.'/11.TXT',$bg1);
			$bg=$this->resizejpg($bg1,$path,$file2,640,1008,$poster['id']);
			//file_put_contents(ROOT_D.'/12.TXT',$bg);
			$bg = $this->createImage($bg);
			imagecopy($target, $bg, 0, 0, 0, 0, 640, 1008);
			imagedestroy($bg);
			$font_file=ROOT_D . '/addons/jy_invitation/resource/msyh.ttf';
			$avatar =$updata['avatar'];
			$d['left']=245; $d['top']=775; $d['width']=150; $d['height']=150;
			$d1['left']=280; $d1['top']=110; $d1['width']=90; $d1['height']=90;
			$d2['left']=280; $d2['top']=215; $d2['size']=16; $d2['color']='#999'; 
			$target = $this->mergeImage($target, $d1, $avatar);
			$target = $this->mergeImage($target, $d, tomedia($qr));
			$updata['nickname']=$updata['nickname']?:'匿名游客';
			$fontBox = imagettfbbox(16, 0, $font_file, $updata['nickname']);
			$textx = ceil((640 - $fontBox[2]) / 2 ); //计算居中位置
			$d2['left']=$textx?:280;			
			$target = $this->mergeText($target, $d2, $updata['nickname']);
			$setting=cfc('jy_weishop')->plugin_setting('shopset');
			$title=$setting['title'];
			$text1=$poster['text1']?$poster['text1']:'向您推荐了一个商城';
			$text2=$poster['text2']?$poster['text2']:$title.'商城：一个特别好用的商城';
			$text3=$poster['text3']?$poster['text3']:'如何做一个好的商城？';
			$text4=$poster['text4']?$poster['text4']:'商城介绍';
			$text5=$poster['text5']?:$setting['desc'];
			$fontBox = imagettfbbox(17, 0, $font_file, $text1);
			$textx = ceil((640 - $fontBox[2]) / 2 ); //计算居中位置
			$text1left=$textx?:210;
			$fontBox = imagettfbbox(15, 0, $font_file, $text2);
			$textx = ceil((640 - $fontBox[2]) / 2 ); //计算居中位置
			$text2left=$textx?:200;
			$fontBox = imagettfbbox(15, 0, $font_file, $text3);
			$textx = ceil((640 - $fontBox[2]) / 2 ); //计算居中位置
			$text3left=$textx?:210;
			$fontBox = imagettfbbox(20, 0, $font_file, $text4);
			$textx = ceil((640 - $fontBox[2]) / 2 ); //计算居中位置
			$text4left=$textx?:210;
			$fontBox = imagettfbbox(15, 0, $font_file, $text5);
			if($fontBox[2]<430){
				$textx = ceil((640 - $fontBox[2]) / 2 ); //计算居中位置
				$text5left=$textx?:100;
			}
			$target = $this->mergeText($target, array('left'=>$text1left,'top'=>280,'color'=>'#e1251b'), $text1);
			$target = $this->mergeText($target, array('left'=>$text2left,'top'=>325,'size'=>15), $text2);
			$target = $this->mergeText($target, array('left'=>$text3left,'top'=>365,'size'=>20), $text3);
			$target = $this->mergeText($target, array('left'=>$text4left,'top'=>465,'size'=>15), $text4);
			$string=$text5;
			$_string='';
			$__string='';
			for($i=0;$i<mb_strlen($string);$i++){
				$box=imagettfbbox(15,0,$font_file,$_string);
				$_string_length=$box[2]-$box[0];
				$box=imagettfbbox(15,0,$font_file,mb_substr($string,$i,1));
				if( $_string_length+$box[2]-$box[0]<430){
					$_string.=mb_substr($string,$i,1);
				}else{
					$__string.=$_string."\n";
					$_string=mb_substr($string,$i,1);
				}
			}
			$__string.=$_string;
			$target = $this->mergeText($target, array('left'=>$text5left?:100,'top'=>505,'size'=>15,'color'=>'#999'), $__string);		
			imagepng($target, $path . $file);
			imagedestroy($target);
		}
		if($_SESSION['fromh5wxapp'] || $_SESSION['os']=='wxapp'){
				$img = 'images/jy_invitation/' . $_SESSION['uniacid'] . '/' .$poster['id'].  '/wxapp/' . $file;
		}else{
				$img = 'images/jy_invitation/' . $_SESSION['uniacid'] . '/' .$poster['id'].  '/mobile/' . $file;
		}
		unlink($path . $file2);
		return $img;
	}
	public function getRealData($data) 
	{
		$data['left'] = intval(str_replace('px', '', $data['left'])) * 2;
		$data['top'] = intval(str_replace('px', '', $data['top'])) * 2;
		$data['width'] = intval(str_replace('px', '', $data['width'])) * 2;
		$data['height'] = intval(str_replace('px', '', $data['height'])) * 2;
		$data['size'] = intval(str_replace('px', '', $data['size'])) * 2;
		$data['src'] = tomedia($data['src']);
		return $data;
	}

}
?>